package com.xmut.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.LongPKService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.xmut.dao.CommentDao;
import com.xmut.entity.Comment;
import com.xmut.entity.Topic;
import com.xmut.filter.CommentFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class CommentService extends LongPKService<Comment> {
    @Autowired
    private CommentDao dao;
    @Autowired
    private TopicService topicService;

    @Override
    protected BaseDao<Comment, Long> getDao() {
        return dao;
    }

    public List<Comment> query(CommentFilter filter) {
        return dao.query(filter);
    }

    /**
     * 发布评论
     * @param comment
     */
    public void saveComment(Comment comment) {
        Topic topic = topicService.getById(comment.getTopicId());
        topic.setCommentNumber(topic.getCommentNumber() == null? 1 : topic.getCommentNumber()+ 1 );
        topicService.update(topic);

        comment.setFloorNum(topic.getCommentNumber());
        comment.setUserId(SysUserUtils.getUserId());
        save(comment);
    }
}
